package dk.gavdi.presentation;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.util.Log;
import android.widget.Toast;

import Exceptions.ConverterException;
import Exceptions.HttpNodeClientException;

public class HttpNodeClient {

	public static JSONObject sendJSON(JSONObject jsonObj) throws HttpNodeClientException {
		HttpResponse response = createPOST(jsonObj);
		return createResponse(response);
	}
	
	private static HttpResponse createPOST(JSONObject jsonObj) throws HttpNodeClientException {
		// Opretter forbindelse
		HttpPost httpPost = new HttpPost("http://172.22.12.127:8080/");
		HttpClient client = new DefaultHttpClient();
		StringEntity entity = null;

		try {
			entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
			entity.setContentType("application/json");
			// Tilknytter objektet og content type til POST.
			httpPost.setEntity(entity);
			return client.execute(httpPost);
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			throw new HttpNodeClientException("HttpNodeClientException/UnsupportedEncodingException - createPOST():" + e.getMessage());
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			throw new HttpNodeClientException("HttpNodeClientException/ClientProtocolException - createPOST():" + e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			throw new HttpNodeClientException("HttpNodeClientException/IOException - createPOST():" + e.getMessage());
		} catch (Exception e)	{
			e.printStackTrace();
			throw new HttpNodeClientException("HttpNodeClientException/Exception - createPOST():" + e.getMessage());
		}
	}

	private static JSONObject createResponse(HttpResponse response) throws HttpNodeClientException {
		JSONObject jsonObj = null;
		// Henter response data fra server vha. httpResponse
		HttpEntity entity1 = response.getEntity();
		if (entity1 != null) {
			InputStream is = null;
			try {
				is = entity1.getContent();
				// convert stream to string
				String result = Converter.convertStreamToString(is);
				
				//Remove []
				//if(result.startsWith("["))
				//	result = result.substring(1, result.length()-1);
				// Create JSON Object
				jsonObj = (JSONObject) new JSONTokener(result).nextValue();

			} catch (IllegalStateException e) {
				e.printStackTrace();
				throw new HttpNodeClientException("HttpNodeClientException/IllegalStateException - createResponse():" + e.getMessage());
			} catch (IOException e) {
				e.printStackTrace();
				throw new HttpNodeClientException("HttpNodeClientException/IOException - createResponse():" + e.getMessage());
			} catch (JSONException e) {
				e.printStackTrace();
				throw new HttpNodeClientException("HttpNodeClientException/JSONException - createResponse():" + e.getMessage());
			} catch (ConverterException e){
				e.printStackTrace();
				throw new HttpNodeClientException("HttpNodeClientException/ConverterException - createResponse():" + e.getMessage());
			}
		}
		return jsonObj;
	}

	

}
